package com.test.threadPool;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DivTask implements Runnable {
	int a, b;

	public DivTask(int a, int b) {
		this.a = a;
		this.b = b;
	}

	public void run() {
		double re = a / b;
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(re);
	}

	public static void main(String[] args) {
		ExecutorService tPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 0L, TimeUnit.MILLISECONDS,
				new SynchronousQueue<Runnable>());
		ExecutorCompletionService<String> service = new ExecutorCompletionService<String>(tPoolExecutor);
		for (int i = 1; i < 5; i++) {
			service.submit(new DivTask(100, i), "success");
		}
		try {
			Future<String> take = service.take();
			Object object = take.get();
			System.out.println(object);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		tPoolExecutor.shutdown();
	}
}
